import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login'
import Home from '../components/Home'
import Wellcome from '../components/Wellcome'
import Users from '../components/user/Users'
import Rights from '../components/power/Rights'
import Roles from '../components/power/Roles'
import Tree from '../components/tree/index'
// import Cate from '../components/goods/Cate'

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  {
    path: '/home',
    component: Home,
    redirect: '/wellcome',
    children: [
      { path: '/wellcome', component: Wellcome },
      { path: '/users', component: Users },
      { path: '/rights', component: Rights },
      { path: '/roles', component: Roles },
      { path: '/categories', component: () => import('@/components/goods/Cate'), }
    ],
  },
  // { path: '/rights', component: Rights },
  { path: '/ValueTransmission', component: () => import('@/components/ValueTransmission/index'), },
  { path: '/Tree', component: () => import('@/components/tree/index'), }

]

const router = new VueRouter({
  mode: 'history',
  routes,
})

//挂在路由守卫，用于监听是否存在token
router.beforeEach((to, from, next) => {
  //to将要访问的路径
  //from代表从哪个路径进来
  //next是一个函数，表示放行
  if (to.path === '/login') return next()
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})

export default router
